package com.hzit.dao;

import com.hzit.bean.Users;
import com.hzit.dao.base.BaseDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UsersDao extends BaseDao {


    /**
     * 根据 用户名  获取 Users 对象
     *
     * @param username
     * @return
     */
    public Users findUsersByUsername(String username) {

        //1.查询
        Connection connection = this.getConnection();
        PreparedStatement preparedStatement = null;
        ResultSet rs = null;


        String sql = "SELECT * FROM users WHERE username=?";

        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, username);

            rs = preparedStatement.executeQuery();

            while (rs.next()) {

                int userid = rs.getInt("userid");
                String username1 = rs.getString("username");
                String sex = rs.getString("sex");
                String pwd = rs.getString("pwd");
                String phone = rs.getString("phone");

                Users users = new Users(userid, username1, pwd, sex, phone);
                return users;
            }


        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.closeAll(rs, preparedStatement, connection);
        }

        System.out.println("------>用户名【" + username + "】不存在");
        return null;
    }


}
